<?php
/**
 * Specs
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/2/9
 * Time: 14:33
 */

namespace app\common\business;

use think\Exception;
use app\common\business\Specs as SpecsBis;

class SpecsValue extends BusBase
{
    public $model = null;
    public function __construct()
    {
        $this->model = new \app\common\model\mysql\SpecsValue();
    }

    /**
     * 获取正常的规格
     */
    public function getBySpecsId($specsId){
        $field = 'id,name';
        try {
            $res = $this->model->getBySpecsId($specsId,$field);
        }catch (\Exception $e){
            //记录日志
            return $e->getMessage();
        }
        $res = $res->toArray();
        return $res;
    }



    /**
     * 删除规格值-软删除
     * @param $specsId
     */
    public function delete($id){
        $res = $this->model->find($id);
        if(!$res){
            throw new Exception('数据不存在');
        }
        $data = [
            'status' => config('status.mysql.table_delete')
        ];
        try{
            $res = $this->model->update($data,['id' => $id]);
        }catch (\Exception $e){
            return false;
        }
        if($res){
            return true;
        }
        return false;
    }

    public function dealGoodsSkus($gids, $flagValue){
        $specsValuKeys = array_keys($gids);
        foreach ($specsValuKeys as $specsValuKey){
            $specsValuKey = explode(',',$specsValuKey);
            foreach ($specsValuKey as $k => $v ){
                $new[$k][] = $v;
                $specsValueIds[] = $v;
            }
        }
//        dump($specsValueIds);die;
        $specsValueIds = array_unique($specsValueIds);
        $specsValues    = $this->getNormalInIds($specsValueIds);
//        dump($specsValues);die;
        $result = [];
        $flagValue = explode(',',$flagValue);
        foreach ($new as $key => $newValue){
            $newValue = array_unique($newValue);
            $list = [];
            foreach ($newValue as $vv){
                $list[] = [
                    'id'    => $vv,
                    'name'  =>  $specsValues[$vv]['name'],
                    'flag'  =>  in_array($vv, $flagValue) ? 1 : ''
                ];
            }
            $result[$key] = [
                'name'  => $specsValues[$newValue[0]]['specs_name'],
                'list'  => $list,
            ];
        }
        return $result;
    }
    public function getNormalInIds($ids){
        try {
            $result = $this->model->getNormalInIds($ids);
        }catch (\Exception $e){
            return [];
        }
        $result = $result->toArray();
        if(!$result){
            return [];
        }
        //获取 specs 值
        $specsName      = (new SpecsBis())->getNormalSpecsAll();
        $specsNameArrs = array_column($specsName,'name','id');

        $res = [];
        foreach ($result as $specsValue){
            $res[$specsValue['id']] = [
                'name'          => $specsValue['name'],
                'specs_name'    => $specsNameArrs[$specsValue['specs_id']] ?? ''
            ];
        }
        return $res;
    }

    public function dealSpecsValue($skusSpecsValueIds){
            $ids = array_values($skusSpecsValueIds);
            $ids = implode(',',$ids);
            $ids = array_unique(explode(',',$ids));

            $result = $this->getNormalInIds($ids);
            if(!$result){
                return [];
            }
            $res = [];
            foreach ($skusSpecsValueIds as $skuId => $specs){
                $specs = explode(',',$specs);
                $specStr = [];
                foreach ($specs as $spec){
                    $specStr[] = $result[$spec]['specs_name'].':'.$result[$spec]['name'];
                }
                $res[$skuId] = implode(' ', $specStr);
            }
            return $res;
    }
}
